package ace.module.account.core.impl.manager.impl;

import ace.cmp.core.constants.CoreConstant;
import ace.cmp.i18n.api.MessageSourceAssertService;
import ace.module.account.api.constant.IamAccountConstant;
import ace.module.account.api.enums.IamApplicationStatusEnum;
import ace.module.account.api.model.dto.IamApplicationDto;
import ace.module.account.core.impl.dao.entity.IamApplication;
import ace.module.account.core.impl.manager.IamApplicationManager;
import ace.module.account.core.impl.manager.cache.IamApplicationCacheManager;
import ace.module.common.core.impl.manager.impl.AbstractApplicationManager;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * .
 *
 * @author caspar
 */
@Slf4j
@AllArgsConstructor
@Component
public class IamApplicationManagerImpl
    extends AbstractApplicationManager<IamApplication, Long, IamApplicationDto>
    implements IamApplicationManager {

  private final IamApplicationCacheManager iamApplicationCacheManager;
  private final MessageSourceAssertService assertService;

  @Override
  public IamApplication getAndCheckById(Long id) {
    IamApplication iamApplication = this.iamApplicationCacheManager.getById(id);
    this.check(iamApplication);
    return iamApplication;
  }

  @Override
  public IamApplication getAndCheckByKey(String key) {
    IamApplication iamApplication = this.iamApplicationCacheManager.getByKey(key);
    this.check(iamApplication);
    return iamApplication;
  }

  @Override
  public IamApplication getAndCheckByOutTypeAndOutId(String outType, String outId) {
    IamApplication iamApplication =
        this.iamApplicationCacheManager.getByOutTypeAndOutId(outType, outId);
    this.check(iamApplication);
    return iamApplication;
  }

  @Override
  public IamApplication getByOutTypeAndOutId(String outType, String outId) {
    return this.iamApplicationCacheManager.getByOutTypeAndOutId(outType, outId);
  }

  @Override
  public IamApplicationDto getDtoByOutTypeAndOutId(String outType, String outId) {
    IamApplication iamApplication = this.getByOutTypeAndOutId(outType, outId);
    IamApplicationDto iamApplicationDto = this.getEntityToDtoConverter().convert(iamApplication);
    return iamApplicationDto;
  }

  @Override
  public IamApplicationDto getAndCheckDtoByOutTypeAndOutId(String outType, String outId) {
    IamApplication iamApplication = this.getAndCheckByOutTypeAndOutId(outType, outId);
    IamApplicationDto iamApplicationDto = this.getEntityToDtoConverter().convert(iamApplication);
    return iamApplicationDto;
  }

  @Override
  public IamApplication getAdminIamApplication() {
    return this.iamApplicationCacheManager.getById(IamAccountConstant.DEFAULT_ADMIN_IAM_APP_ID);
  }

  @Override
  public IamApplication getAndCheckByNotNullAllAdminIamApplicationAndDeleteFlagIsFalse() {
    IamApplication iamApplication = this.getAdminIamApplication();
    this.checkNotNull(iamApplication);
    return iamApplication;
  }

  private void check(IamApplication iamApplication) {
    this.checkNotNull(iamApplication);
    assertService.isTrue(
        !CoreConstant.DELETE_FLAG_NOT.equals(iamApplication.getDeleteFlag()),
        IamApplication.class.getName() + ".not-deleted-error",
        "账户应用不存在");
    assertService.isFalse(
        IamApplicationStatusEnum.NORMAL.getCode().equals(iamApplication.getStatus()),
        IamApplication.class.getName() + ".not-lock-error",
        "账户应用已锁定");
  }

  private void checkNotNull(IamApplication iamApplication) {
    assertService.isNull(iamApplication, IamApplication.class.getName() + ".not-exist-error", "账户应用不存在");
  }
}
